/*
 * TarifaSocialApplication.java
 *
 * Created on 8 de noviembre de 2006, 11:59
 *
 * Copyright(c) 2006 FIEL (Fundaci�n de Investigaciones Econ�micas Latinoamericanas).
 * All Rights Reserved.
 * This software is the proprietary information of FIEL.
 */
package ar.org.fiel.tarifasocial.ui;

import ar.org.fiel.tarifasocial.om.model.Parameter;
import ar.org.fiel.tarifasocial.om.model.User;
import ar.org.fiel.tarifasocial.services.Configuration;
import ar.org.fiel.tarifasocial.services.Persistence;
import ar.org.fiel.tarifasocial.ui.pages.pub.PageHome;
import java.util.List;
import javax.naming.NameNotFoundException;
import org.apache.log4j.Logger;
import org.apache.torque.TorqueException;
import wicket.ISessionFactory;
import wicket.Session;
import wicket.protocol.http.WebApplication;

/**
 * @author Ing. Luis Pablo Gallo (luispablo.gallo@gmail.com)
 */
public class TarifaSocialApplication extends WebApplication
{

    private static User rootUser;

    public static final String ROOT_USER_NAME = "rootUserName";
    public static final String ROOT_PASSWORD = "rootPassword";

    private Logger logger = Logger.getLogger(TarifaSocialApplication.class);

    public Class getHomePage()
    {
        return PageHome.class;
    }

    @Override
    public ISessionFactory getSessionFactory()
    {
        return new ISessionFactory()
        {

            public Session newSession()
            {
                return new TarifaSocialSession(TarifaSocialApplication.this);
            }
        };
    }

    private void loadParameters()
    {
        // Get all parameter codes.
        List<String> parameterCodes = Configuration.getParameterCodes();
        Configuration configuration = new Configuration();

        for (String parameterCode : parameterCodes)
        {
            try
            {
                // Try to get the parameter.
                Parameter parameter = configuration.getParameter(parameterCode);

                // If the parameter is not set.
                if (parameter.getValue() == null)
                {
                    // Try to get it from web.xml.
                    String value = this.getWicketServlet().getInitParameter(parameterCode);

                    // If it's there, set it.
                    if (value != null)
                    {
                        parameter.setValue(value);
                    }

                    parameter.save();
                }
            }
            catch (TorqueException exception)
            {
                if (exception.getCause() instanceof NameNotFoundException)
                {
                    logger.warn("Torque found this: " + exception.getCause().getMessage());
                }
            }
            catch (Exception exception)
            {
                exception.printStackTrace();
            }
        }
    }

    @Override
    protected void init()
    {
        String rootUserName = this.getWicketServlet().getInitParameter(ROOT_USER_NAME);
        String rootPassword = this.getWicketServlet().getInitParameter(ROOT_PASSWORD);

        if (rootUserName == null || rootPassword == null)
        {
            logger.fatal("Cannot create root user! Not properly configured.");
            return;
        }

        try
        {
            Persistence persistence = new Persistence();
            User rootUser = persistence.checkRootUserAvailable(rootUserName, rootPassword);

            TarifaSocialApplication.setRootUser(rootUser);
        }
        catch (TorqueException exception)
        {
            if (exception.getCause() instanceof NameNotFoundException)
            {
                logger.warn("Torque found this: " + exception.getCause().getMessage());
            }
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
            logger.error("ERROR", exception);
        }

        this.loadParameters();
    }

    public static User getRootUser()
    {
        return rootUser;
    }

    public static void setRootUser(User aRootUser)
    {
        rootUser = aRootUser;
    }
}